// 过滤obj,用于获取一个obj只想要的字段
export function getWantObject (arr, data) {
	let obj = {}

	arr.map(item => {
		obj[item] = data[item]
	})

	return obj
}

export function diyLiuDownload (json, name) {
  var a = document.createElement('a') // 创建一个<a></a>标签
  a.href = URL.createObjectURL(json)
  a.download = name // 文件名称
  a.style.display = 'none'
  document.body.appendChild(a)
  a.click()
  a.remove()
}

// 下载方法，自定义名字(url为文件地址)
export function diyDownload (url, name) {
  fetch(url).then(res =>
    res.blob().then(blob => {
      var a = document.createElement('a')
      var url = window.URL.createObjectURL(blob)
      var filename = name
      a.href = url
      a.download = filename
      a.click()
      window.URL.revokeObjectURL(url)
    })
  )
}

// 过滤时间格式
export function timeFilter (time, format) {
	var o = {
		'M+': time.getMonth() + 1, // month
		'd+': time.getDate(), // day
		'H+': time.getHours(), // hour
		'm+': time.getMinutes(), // minute
		's+': time.getSeconds(), // second
		'q+': Math.floor((time.getMonth() + 3) / 3), // quarter
		'S': time.getMilliseconds() // millisecond
	}

	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (time.getFullYear() + '').substr(4 - RegExp.$1.length))
	}

	for (var k in o) {
		if (new RegExp('(' + k + ')').test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
		}
	}
	return format
}

function filterStr (val) {
	return val || '--'
}

// 过滤台风路径基础信息
export function filterTyphoonBaseInfo (type, info) {
	let str = '', title = ''

	if (type === 'typhooned') {
		title = `实时台风路径信息 - <span class="red">${filterStr(info.strong)}</span>`
		str = `
			<span class="typhoon-baseTip-key">台风名称：</span>${filterStr(info.tfbh)} - ${filterStr(info.typhoon_name)}<br />
			<span class="typhoon-baseTip-key">过去时间：</span>${filterStr(timeFilter(new Date(info.time.replace('T', ' ')), 'yyyy年MM月dd日 HH时'))}<br />
			<span class="typhoon-baseTip-key">中心位置：</span>${filterStr(info.longitude)} °E，${filterStr(info.latitude)} °N<br />
			<span class="typhoon-baseTip-key">最大风力：</span>${filterStr(info.power)} 级<br />
			<span class="typhoon-baseTip-key">最大风速：</span>${filterStr(info.speed)} 米/秒<br />
			<span class="typhoon-baseTip-key">中心气压：</span>${filterStr(info.pressure)} 百帕<br />
			<span class="typhoon-baseTip-key">移动速度：</span>${filterStr(info.move_speed)} 公里/小时<br />
			<span class="typhoon-baseTip-key">移动方向：</span>${filterStr(info.move_dir)}<br />
			<span class="typhoon-baseTip-key">七级风圈半径：</span>${filterStr(info.radius7)} 公里<br />
			<span class="typhoon-baseTip-key">十级风圈半径：</span>${filterStr(info.radius10)} 公里<br />
			<span class="typhoon-baseTip-key">十二级风圈半径：</span>${filterStr(info.radius12)} 公里<br />
		`
	} else {
		title = '预测台风路径信息'
		str = `
			<span class="typhoon-baseTip-key">台风名称：</span>${filterStr(info.tfbh)} - ${filterStr(info.typhoon_name)}<br />
			<span class="typhoon-baseTip-key">预报机构：</span>${filterStr(info.sets)}<br />
			<span class="typhoon-baseTip-key">到达时间：</span>${filterStr(timeFilter(new Date(info.time.replace('T', ' ')), 'yyyy年MM月dd日 HH时mm分ss秒'))}<br />
			<span class="typhoon-baseTip-key">中心位置：</span>${filterStr(info.longitude)} °E，${filterStr(info.latitude)} °N<br />
			<span class="typhoon-baseTip-key">最大风力：</span>${filterStr(info.power)} 级<br />
			<span class="typhoon-baseTip-key">最大风速：</span>${filterStr(info.speed)} 米/秒<br />
			<span class="typhoon-baseTip-key">中心气压：</span>${filterStr(info.pressure)} 百帕<br />
			<span class="typhoon-baseTip-key">移动速度：</span>${filterStr(info.move_speed)} 公里/小时<br />
			<span class="typhoon-baseTip-key">移动方向：</span>${filterStr(info.move_dir)}<br />
		`
	}
	return {
		title,
		str
	}
}
